package com.bilibili.controller;

import com.bilibili.enumeration.RegisterType;
import com.bilibili.form.LoginForm;
import com.bilibili.form.NewPasswordForm;
import com.bilibili.form.RegisterForm;
import com.bilibili.form.UpdatePasswordForm;
import com.bilibili.service.UserService;
import com.bilibili.util.RSAUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

@Api(tags = "认证服务")
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/auth")
public class AuthController {

    private final UserService userService;

    @ApiOperation("获取公钥")
    @GetMapping("/publicKey")
    public String getPublicKey(){
        return RSAUtil.getPublicKeyStr();
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public String register(@ApiParam(value = "注册表单", required = true) @Valid @RequestBody RegisterForm form){
        RegisterType type = form.getType();
        if (type.equals(RegisterType.PHONE)) {
            if (StringUtils.isBlank(form.getPhone())) {
                throw new RuntimeException("手机号不能为空");
            }
            return userService.registerByPhone(form.getPhone(), form.getVerifyCode());
        } else if(type.equals(RegisterType.EMAIL)){
            if (StringUtils.isBlank(form.getEmail())) {
                throw new RuntimeException("邮箱不能为空");
            }
            return userService.registerByEmail(form.getEmail(), form.getVerifyCode());
        } else {
            throw new RuntimeException("非法请求");
        }
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public String login(@ApiParam(value = "登录表单", required = true) @Valid @RequestBody LoginForm form){
        return userService.login(form.getAccount(), form.getPassword(), form.getVerifyCode(), form.getType());
    }

    @ApiOperation("获取短信验证码")
    @GetMapping("/verify")
    public String getVerifyCode(@ApiParam(value = "手机号", required = true) @NotBlank(message = "手机号不能为空") @RequestParam String phone){
        return "";
    }

    @ApiOperation("添加密码")
    @PutMapping("/addPassword")
    public void addPassword(@ApiParam(value = "添加密码表单", required = true) @Valid @RequestBody NewPasswordForm form){
        userService.addPassword(2L, form.getPassword(), form.getConfirmPassword());
    }

    @ApiOperation("更新密码")
    @PutMapping("/updatePassword")
    public void updatePassword(@ApiParam(value = "更新密码表单", required = true) @Valid @RequestBody UpdatePasswordForm form){
        userService.updatePassword(2L, form.getOldPassword(), form.getNewPassword(), form.getConfirmPassword());
    }

    @ApiOperation("加密密码")
    @GetMapping("/test/encryptPassword")
    public String encryptPassword(@ApiParam(value = "明文密码", required = true) @NotBlank(message = "密码不能为空") @RequestParam String password){
        return RSAUtil.encrypt(password);
    }



}
